package es.udc.pfc.davidparedes.web.pages.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PageRenderLinkSource;

import es.udc.pfc.davidparedes.model.store.Store;
import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.universe.Universe;
import es.udc.pfc.davidparedes.model.universemanager.UniverseManager;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;

import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.DIRECTOR)
public class UpdateUniverse {
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private StoreService storeService;
    
    @Property
    private String universeName;
    
    @Property
    private Long universeId;
    
    @Property
    private UniverseManager manager;
    
    @Property
    private List<UniverseManager> managers;
    
    @Component
    private Form updateUniverseForm;
    
    @Inject 
    private PageRenderLinkSource pageLinkSource;
    
    void onActivate(Long id) throws InstanceNotFoundException {
    	Universe universe = storeService.findUniverse(id);
    	universeId = id;
    	
    	universeName = universe.getName();
    	
    	managers = new ArrayList<UniverseManager>();
    	Store store = userService.findUserProfile(userSession.getUserProfileId()).getStore();
    	managers.addAll(storeService.getStoreManagers(store.getStoreId()));
    	
    	manager = universe.getManager();
    	
    }

    Object onValidateFromUpdateUniverseForm() throws NumberFormatException, InstanceNotFoundException {

        if (!updateUniverseForm.isValid()) {
            return this;
        }
        
        Universe universe = storeService.findUniverse(universeId);
        
        storeService.updateUniverse(universe.getUniverseId(), universeName, universe.getStore().getStoreId());
        
        if (manager != null) {
        	userService.moveUniverseManager(manager.getUserProfileId(), universe.getUniverseId());
        }
        
        return pageLinkSource.createPageRenderLinkWithContext("user/UniverseStats", universe.getUniverseId());
    }
    
    public ValueEncoder<UniverseManager> getUniverseManagerEncoder() {

        return new ValueEncoder<UniverseManager>() {

            @Override
            public String toClient(UniverseManager value) {
                return String.valueOf(value.getUserProfileId()); 
            }

            @Override
            public UniverseManager toValue(String id) { 
            	
            	UniverseManager manager = new UniverseManager();
                try {
					manager = userService.findUniverseManager(Long.parseLong(id));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstanceNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                
                return manager;
            }
        };
        
    }

}
